package com.lx.intercept;

import com.lx.utill.JwtUtill;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        String token = request.getHeader("token");
//        System.out.println(token);
//        System.out.println("拦截处理");
        if (JwtUtill.VerifyToken(token) && token != null) {
            return true;
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print("<script>alert('token验证失败');" +
                "window.localStorage.removeItem(\"token\");" +
                "window.location.href='/';" +
                "</script>");
        return false;
    }
}
